package com.frogs.ucenter.util.task;

import java.util.Arrays;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.frogs.system.web.api.entity.TaskActionMessageMqEntity;
import com.frogs.ucenter.component.GivingAsyncHelper;
import com.frogs.ucenter.dao.ActionLogDao;
import com.frogs.ucenter.entity.ActionLog;



/**
 * 接收队列消息，通过记录任务动作
 * @author xiaohei
 *
 */
@Component("taskActionMqReceive")
public class TaskActionMqReceive {
	
	private static final Logger log = LoggerFactory.getLogger(TaskActionMqReceive.class);
	
	@Resource
	private ActionLogDao actionLogDao;
	@Resource
	private GivingAsyncHelper givingAsyncHelper;
	
	/**
	 * 发送队列消息到任务日志
	 */
	public void addActionLog(TaskActionMessageMqEntity taskActionMessageMqEntity){
		ActionLog actionLog = new ActionLog();
		actionLog.setActionType(taskActionMessageMqEntity.getActionType());
		actionLog.setMoney(taskActionMessageMqEntity.getMoney());
		actionLog.setUserId(taskActionMessageMqEntity.getUserId());
		actionLog.setRemark(taskActionMessageMqEntity.getRemark());
		actionLog.setOpenPlatformId(taskActionMessageMqEntity.getOpenPlatformId());
		actionLogDao.insert(actionLog);
		
		try {
			// 自动更新常规任务
			givingAsyncHelper.autoUpdateTask(taskActionMessageMqEntity.getActionType(), taskActionMessageMqEntity.getUserId(), taskActionMessageMqEntity.getOpenPlatformId());
		} catch (Exception e) {
			log.error("func[{}] taskActionMessageMqEntity[{}] exception[{} - {}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
					JSON.toJSONString(taskActionMessageMqEntity), e.getMessage(),Arrays.deepToString(e.getStackTrace()) });
		}
	}
	
}
